using System.Windows.Controls;
using FileTransferDesktop.Views.Forms;
using Wpf.Ui.Controls;

namespace FileTransferDesktop.Views.Pages
{
    public partial class WinFormsIntegrationPage : Page
    {
        private WinFormsExampleControl? _winFormsControl;

        public WinFormsIntegrationPage()
        {
            InitializeComponent();
            InitializeWinFormsControl();
        }

        private void InitializeWinFormsControl()
        {
            try
            {
                // Create WinForms control instance
                _winFormsControl = new WinFormsExampleControl();

                // Subscribe to events from WinForms control
                _winFormsControl.DataChanged += WinFormsControl_DataChanged;

                // Set the child of WindowsFormsHost
                WinFormsHost.Child = _winFormsControl;

                UpdateStatus("WinForms 控件已成功加载", true);
            }
            catch (Exception ex)
            {
                UpdateStatus($"加载 WinForms 控件失败: {ex.Message}", false);
            }
        }

        private void WinFormsControl_DataChanged(object? sender, string data)
        {
            // Handle data changes from WinForms control
            Dispatcher.Invoke(() =>
            {
                UpdateStatus($"从 WinForms 接收到数据: {data}", true);
            });
        }

        private void SendToWinFormsButton_Click(object sender, RoutedEventArgs e)
        {
            if (_winFormsControl != null && !string.IsNullOrWhiteSpace(WpfTextBox.Text))
            {
                try
                {
                    // Send data to WinForms control
                    _winFormsControl.UpdateFromWPF(WpfTextBox.Text);
                    UpdateStatus($"已发送消息到 WinForms: {WpfTextBox.Text}", true);

                    // Clear the textbox
                    WpfTextBox.Clear();
                }
                catch (Exception ex)
                {
                    UpdateStatus($"发送消息失败: {ex.Message}", false);
                }
            }
            else
            {
                UpdateStatus("请输入要发送的消息", false);
            }
        }

        private void RefreshButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // Refresh WinForms control
                if (_winFormsControl != null)
                {
                    _winFormsControl.UpdateFromWPF($"刷新时间: {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
                    UpdateStatus("WinForms 控件已刷新", true);
                }
            }
            catch (Exception ex)
            {
                UpdateStatus($"刷新失败: {ex.Message}", false);
            }
        }

        private void UpdateStatus(string message, bool isSuccess)
        {
            StatusTextBlock.Text = $"状态: {message}";

            if (StatusBadge != null)
            {
                StatusBadge.Severity = isSuccess
                    ? InfoBadgeSeverity.Success
                    : InfoBadgeSeverity.Critical;
            }
        }

        // Cleanup when page is unloaded
        //protected override void OnUnloaded(RoutedEventArgs e)
        //{
        //    base.OnUnloaded(e);

        //    if (_winFormsControl != null)
        //    {
        //        _winFormsControl.DataChanged -= WinFormsControl_DataChanged;
        //        _winFormsControl.Dispose();
        //        _winFormsControl = null;
        //    }
        //}
    }
}
